package com.triones.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.exception.ServiceException;
import com.triones.api.entity.RichContentDO;
import com.triones.api.mapper.RichContentMapper;
import com.triones.api.service.RichContentService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 富文本 -业务实现
 */
@Service
@RequiredArgsConstructor
public class RichContentServiceImpl implements RichContentService {

    private final RichContentMapper richContentMapper;

    @Override
    public RichContentDO selectById(String type) {
        return richContentMapper.selectOne(new LambdaQueryWrapper<RichContentDO>().eq(RichContentDO::getType, type).last("limit 1"));
    }

    @Override
    public boolean insert(RichContentDO dto) {
        RichContentDO contentDO = richContentMapper.selectOne(new LambdaQueryWrapper<RichContentDO>().eq(RichContentDO::getType, dto.getType()));
        if (contentDO != null) {
            throw new ServiceException("该类型的内容已存在");
        }
        dto.setCreateAt(System.currentTimeMillis());
        return richContentMapper.insert(dto) > 0;
    }

    @Override
    public boolean update(RichContentDO dto) {
        dto.setUpdateAt(System.currentTimeMillis());
        return richContentMapper.updateById(dto) > 0;
    }
}
